package club.hicode.daydayup.clean;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.FileUtil;
import com.google.common.io.Files;
import org.junit.Test;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * XXXX
 *
 * @author LiuChunfu
 * @date 2018/5/5
 */
public class GuPaoClean {

    private static final List<String> DIRS = Arrays.asList(".settings", "bin", ".svn", "target", ".idea", "out");

    private static final List<String> FILES = Arrays.asList(".classpath", ".project");

    private static final List<String> FILES_STUFFIX = Arrays.asList("iml");


    private static final String BASE_PATH = "C:\\Users\\LiuCh\\Downloads\\public";

    @Test
    public void clean() {
        Iterable<File> files = Files.fileTraverser().breadthFirst(new File(BASE_PATH));
        List<File> filsList = CollectionUtil.newArrayList(files);
        Set<File> collect = filsList.stream().filter(f -> fileter(f)).collect(Collectors.toSet());
        collect.forEach(FileUtil::del);
        // collect.forEach(System.out::println);
    }

    private boolean fileter(File file) {
        String name = file.getName();
        if (file.isDirectory()) {
            return DIRS.contains(name);
        } else {
            return FILES.contains(name) || FILES_STUFFIX.contains(getStuffix(name));
        }
    }

    private String getStuffix(String fileName) {
        String[] split = fileName.split("\\.");
        if (split.length == 2) {
            return split[1];
        }
        return "";
    }

}
